#include "pagridplot.h"

int default_log(logFctrl* ctrl){
  ctrl->F_stats      = ctrl->labels->F_stats;
  ctrl->F_plot       = ctrl->labels->F_stdout;
  ctrl->plot_ext     = STDOUT;
  ctrl->plotstyle    = ctrl->labels->xy;
  ctrl->part         = ctrl->labels->before_ini;
  ctrl->multiple_win = NO;
  ctrl->nb_bars      = 10;
  
  ctrl->x            = LOG_CUTEDGE;
  ctrl->x_stat       = -1;
  ctrl->y            = LOG_MAXWEIGHT;
  ctrl->y_stat       = -1;
  ctrl->z            = LOG_CUTEDGE;
  ctrl->z_stat       = -1;
  
  ctrl->matching     = 0;
  ctrl->group        = BY_COARSENING;

  sprintf(ctrl->labels->F_plot,".bi%s%s",ctrl->labels->file[ctrl->x],
	  ctrl->labels->file[ctrl->y]);

  return 1;
}

int menufn_part(menu_info* info){
  int option = info->option;
  logFctrl* ctrl;

  WARNING((info->info1 == NULL,"menufn_part  error 1 : info->info1 undefined\n"),0);
  ctrl = (logFctrl*)info->info1;
  
  switch(option){
  case 1:
    ctrl->part = ctrl->labels->before_ini;
    if(ctrl->x == 40) /* imbalance factor for x */
      ctrl->x = 35;
    break;
  case 2:
    ctrl->part = ctrl->labels->after_ini;
    break;    
  }
  
  if(ctrl->plotstyle == ctrl->labels->xy){
    if(ctrl->part == ctrl->labels->before_ini)
      sprintf(ctrl->labels->F_plot,".bi%s%s",ctrl->labels->file[ctrl->x],ctrl->labels->file[ctrl->y]);
    else
      sprintf(ctrl->labels->F_plot,"ai%s%s",ctrl->labels->file[ctrl->x],ctrl->labels->file[ctrl->y]);
  }else if(ctrl->plotstyle == ctrl->labels->xyz){
    if(ctrl->part == ctrl->labels->before_ini)
      sprintf(ctrl->labels->F_plot,".bi%s%s%s",ctrl->labels->file[ctrl->x],
	      ctrl->labels->file[ctrl->y],ctrl->labels->file[ctrl->z]);
    else
      sprintf(ctrl->labels->F_plot,".ai%s%s%s",ctrl->labels->file[ctrl->x],
	      ctrl->labels->file[ctrl->y],ctrl->labels->file[ctrl->z]);
  }else{
    if(ctrl->part == ctrl->labels->before_ini)
      sprintf(ctrl->labels->F_plot,".bi%s",ctrl->labels->file[ctrl->x]);
    else
      sprintf(ctrl->labels->F_plot,".ai%s",ctrl->labels->file[ctrl->x]);
  }

  return 1;
}

int menufn_plot_ext(menu_info* info){
  int option = info->option;
  logFctrl* ctrl;

  WARNING((info->info1 == NULL,"menufn_plot_ext  error 1 : info->info1 undefined\n"),0);
  ctrl = (logFctrl*)info->info1;
  if(option == STDOUT || option == PDF || option == EPS || option == PS || option == PNG){
    ctrl->plot_ext = option;
    ctrl->F_plot = (option == STDOUT?ctrl->labels->F_stdout:ctrl->labels->F_plot);
  }
  
  return 1;
}

int menufn_F_stat(menu_info* info){
  int option = info->option;
  logFctrl* ctrl;

  WARNING((info->info1 == NULL,"menufn_F_stat error 1 : info->info1 undefined\n"),0);
  ctrl = (logFctrl*)info->info1;

  switch(option){
  case 1:
    ctrl->F_stats = ctrl->labels->F_stdout;
    break;
  case 2:
    ctrl->F_stats = ctrl->labels->F_stats;
    break;
  }
  return 1;
}

int menufn_group(menu_info* info){
  int option = info->option;
  logFctrl* ctrl;

  WARNING((info->info1 == NULL,"menufn_plot_ext  error 1 : info->info1 undefined\n"),0);
  ctrl = (logFctrl*)info->info1;
  
  if(option != BY_MATCHING && option != BY_COARSENING)
    return 1;

  ctrl->group = option;
  return 1;
}
int menufn_values(menu_info* info){
  int option = info->option;
  logFctrl* ctrl;
  int *val;
  int *val_type;
  int *val_stat;
  int choice;

  WARNING((info->info1 == NULL,"menufn_values error 1 : info->info1 undefined\n"),0);
  WARNING((info->info2 == NULL,"menufn_values error 2 : info->info2 undefined\n"),0);
  WARNING((info->info3 == NULL,"menufn_values error 3 : info->info3 undefined\n"),0);
  WARNING((info->info4 == NULL,"menufn_values error 4 : info->info4 undefined\n"),0);
  WARNING((info->info5 == NULL,"menufn_values error 5 : info->info5 undefined\n"),0);
  ctrl = (logFctrl*)info->info1;
  val  = (int*)info->info2;
  val_type = (int*)info->info3;
  val_stat = (int*)info->info4;  
  choice = *((int*)info->info5);  

  if(option != LOG_CUTEDGE && option != LOG_TOTCOMMCOST && 
     option != LOG_MAXWEIGHT && option != LOG_CUTEDGEBYTOTCOMMCOST && 
     option != LOG_IMBALANCE && option != LOG_MAXEXECTIME){
    if(ctrl->part == ctrl->labels->before_ini && option == 40)
      return 1;
    if(option != 11 && option != 12 && option != 13 && option != 14 && 
       option != 15 && option != 21 && option != 22 && option != 23 && 
       option != 24 && option != 25 && option != 31 && option != 32 && 
       option != 33 && option != 34 && option != 35 && option != 71 && 
       option != 72 && option != 73 && option != 74 && option != 75)
      return 1;
    else if((choice == 5 && ctrl->plotstyle == ctrl->labels->xy) || choice == 6)
      return 1;    
  }  
  
  *val = option;
  *val_type = -1;
  *val_stat = -1;
  if((int)(*val)/10 != 0){
    *val_type = (int)((*val)/10);
    *val_stat = (*val) % 10;
  }
  
  if(ctrl->plotstyle == ctrl->labels->xy){
    if(ctrl->part == ctrl->labels->before_ini)
      sprintf(ctrl->labels->F_plot,".bi%s%s",ctrl->labels->file[ctrl->x],ctrl->labels->file[ctrl->y]);
    else
      sprintf(ctrl->labels->F_plot,".ai%s%s",ctrl->labels->file[ctrl->x],ctrl->labels->file[ctrl->y]);
  }else if(ctrl->plotstyle == ctrl->labels->xyz){
    if(ctrl->part == ctrl->labels->before_ini)
      sprintf(ctrl->labels->F_plot,".bi%s%s%s",ctrl->labels->file[ctrl->x],ctrl->labels->file[ctrl->y],
	      ctrl->labels->file[ctrl->z]);
    else
      sprintf(ctrl->labels->F_plot,".ai%s%s%s",ctrl->labels->file[ctrl->x],ctrl->labels->file[ctrl->y],
	      ctrl->labels->file[ctrl->z]);
  }else{
    if(ctrl->part == ctrl->labels->before_ini)
      sprintf(ctrl->labels->F_plot,".bi%s",ctrl->labels->file[ctrl->x]);
    else
      sprintf(ctrl->labels->F_plot,".ai%s",ctrl->labels->file[ctrl->x]);
  }
  return 1;
}

int menufn_plotstyle(menu_info* info){
  int option = info->option;
  logFctrl* ctrl;

  WARNING((info->info1 == NULL,"menufn_plotstyle error 1 : info->info1 undefined\n"),0);
  ctrl = (logFctrl*)info->info1;

  switch(option){
  case 1:
    ctrl->plotstyle = ctrl->labels->distrib;
    ctrl->multiple_win = YES;
    if(ctrl->part == ctrl->labels->before_ini){
      sprintf(ctrl->labels->F_plot,".bi%s",ctrl->labels->file[ctrl->x]);
    }else{
      sprintf(ctrl->labels->F_plot,".ai%s",ctrl->labels->file[ctrl->x]);
    }
    break;
  case 2:
    ctrl->plotstyle = ctrl->labels->xy;
    ctrl->multiple_win = NO;
    if(ctrl->part == ctrl->labels->before_ini){
      sprintf(ctrl->labels->F_plot,".bi%s%s",ctrl->labels->file[ctrl->x],
	      ctrl->labels->file[ctrl->y]);
    }else{
      sprintf(ctrl->labels->F_plot,".ai%s%s",ctrl->labels->file[ctrl->x],
	      ctrl->labels->file[ctrl->y]);
    }    
    break;
  case 3:
    ctrl->plotstyle = ctrl->labels->xyz;
    ctrl->multiple_win = NO;
    if(ctrl->part == ctrl->labels->before_ini){
      sprintf(ctrl->labels->F_plot,".bi%s%s%s",ctrl->labels->file[ctrl->x],
	      ctrl->labels->file[ctrl->y],ctrl->labels->file[ctrl->z]);
    }else{
      sprintf(ctrl->labels->F_plot,".ai%s%s%s",ctrl->labels->file[ctrl->x],
	      ctrl->labels->file[ctrl->y],ctrl->labels->file[ctrl->z]);
    }
    break;
  }
  return 1;
}

int menufn_logfirst(menu_info* info){
  int option = info->option;
  logFctrl* ctrl;

  WARNING((info->info1 == NULL,"menufn_logfirst error 1 : info->info1 undefined\n"),0);
  ctrl = (logFctrl*)info->info1;

  switch(option){
  case 0:
    plotstyle_menu(&(menufn_plotstyle),info);
    break;
  case 1:
    F_stat_menu(&(menufn_F_stat),info);
    break;
  case 2:
    plot_ext_menu(&(menufn_plot_ext),info);
    break;
  case 3:
    part_menu(&(menufn_part),info);
    break;
  case 4:
    info->info2 = &(ctrl->x);
    info->info3 = &(ctrl->x_type);
    info->info4 = &(ctrl->x_stat); 
    info->info5 = &option;
    values_menu(&(menufn_values),info);
    break;
  case 5:
    info->info2 = &(ctrl->y);
    info->info3 = &(ctrl->y_type);
    info->info4 = &(ctrl->y_stat);              
    info->info5 = &option;
    values_menu(&(menufn_values),info);
    break;
  case 6:
    if(ctrl->plotstyle != ctrl->labels->xyz)
      break;
      
    info->info2 = &(ctrl->z);
    info->info3 = &(ctrl->z_type);
    info->info4 = &(ctrl->z_stat);
 
    info->info5 = &option;
    values_menu(&(menufn_values),info);    
    break;
  case 7:
    printf("Enter number of bars : ");
    while(scanf("%d",&option)!=1){
      getchar();      
    }
    ctrl->nb_bars = option;
    break;
  case 8:
    group_menu(&(menufn_group),info);
    break;
  }

  return 1;
}
int menufn_main(menu_info* info){
  int option = info->option;
  int coords;
  logFinfo* loginfo;

  WARNING((info->info1 == NULL,"menufn_main error 1 : info->info1 undefined\n"),0);
  WARNING((info->info2 == NULL,"menufn_main error 2 : info->info2 undefined\n"),0);
  coords = *((int*)info->info1);
  loginfo = (logFinfo*)info->info2;

#ifdef DEBUG
  dbg_freemem();
#endif
  switch(option){
  case 1:
    //timingfile(base);
    break;
  case 2:
    //logfile(base,BEFORE_ET);
    break;
  case 3:
    logfiles(loginfo,AFTER_ET);
    break;
  case 4:
    //matchstats(base);
    break;
  case 5:
    if(coords){
      //dxoutput(base,coords);
    }
    break;
  case 6:
    //refinestats(base);
    break;
  case 7:
    if(coords){
      //refinedxoutput(base,coords);
    }
    break;
  case 8:
    if(coords){
      //geomviewapp(base,coords);
    }
    break;
  case 9:
    if(coords){
      //geomviewpart(base,coords);
    }
    break;
  }  
#ifdef DEBUG
  dbg_isemptymem();
#endif
  return 1;
}
